<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html lang="zh-CN"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta http-equiv="Content-Language" content="zh-CN"><link href="stylesheet.css" media="all" rel="stylesheet" type="text/css">
<title>数组函数和操作符</title>
<script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?d286c55b63a3c54a1e43d10d4c203e75"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script>
</head><body class="SECT1">
<div>
<table summary="Header navigation table" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><th colspan="5" align="center" valign="bottom">PostgreSQL 8.2.3 中文文档</th></tr>
<tr><td width="10%" align="left" valign="top"><a href="functions-conditional.html" accesskey="P">后退</a></td><td width="10%" align="left" valign="top"><a href="functions.html">快退</a></td><td width="60%" align="center" valign="bottom">章9. 函数和操作符</td><td width="10%" align="right" valign="top"><a href="functions.html">快进</a></td><td width="10%" align="right" valign="top"><a href="functions-aggregate.html" accesskey="N">前进</a></td></tr>
</table>
<hr align="LEFT" width="100%"></div>
<div class="SECT1"><h1 class="SECT1"><a name="FUNCTIONS-ARRAY">9.14. 数组函数和操作符</a></h1>
<p><a href="functions-array.html#ARRAY-OPERATORS-TABLE">表9-35</a>显示了可以用于 <tt class="TYPE">array</tt> 类型的操作符。</p>
<div class="TABLE"><a name="ARRAY-OPERATORS-TABLE"></a>
<p><b>表9-35. <tt class="TYPE">array</tt> 操作符</b></p>
<table border="1" class="CALSTABLE"><col><col><col><col>
<thead>
<tr><th>操作符</th><th>描述</th><th>例子</th><th>结果</th></tr>
</thead>
<tbody>
<tr><td><tt class="LITERAL">=</tt></td><td>等于</td><td><tt class="LITERAL">ARRAY[1.1,2.1,3.1]::int[] = ARRAY[1,2,3]</tt></td><td><tt class="LITERAL">t</tt></td></tr>
<tr><td><tt class="LITERAL">&lt;&gt;</tt></td><td>不等于</td><td><tt class="LITERAL">ARRAY[1,2,3] &lt;&gt; ARRAY[1,2,4]</tt></td><td><tt class="LITERAL">t</tt></td></tr>
<tr><td><tt class="LITERAL">&lt;</tt></td><td>小于</td><td><tt class="LITERAL">ARRAY[1,2,3] &lt; ARRAY[1,2,4]</tt></td><td><tt class="LITERAL">t</tt></td></tr>
<tr><td><tt class="LITERAL">&gt;</tt></td><td>大于</td><td><tt class="LITERAL">ARRAY[1,4,3] &gt; ARRAY[1,2,4]</tt></td><td><tt class="LITERAL">t</tt></td></tr>
<tr><td><tt class="LITERAL">&lt;=</tt></td><td>小于或等于</td><td><tt class="LITERAL">ARRAY[1,2,3] &lt;= ARRAY[1,2,3]</tt></td><td><tt class="LITERAL">t</tt></td></tr>
<tr><td><tt class="LITERAL">&gt;=</tt></td><td>大于或等于</td><td><tt class="LITERAL">ARRAY[1,4,3] &gt;= ARRAY[1,4,3]</tt></td><td><tt class="LITERAL">t</tt></td></tr>
<tr><td><tt class="LITERAL">@&gt;</tt></td><td>包含</td><td><tt class="LITERAL">ARRAY[1,4,3] @&gt; ARRAY[3,1]</tt></td><td><tt class="LITERAL">t</tt></td></tr>
<tr><td><tt class="LITERAL">&lt;@</tt></td><td>被包含于</td><td><tt class="LITERAL">ARRAY[2,7] &lt;@ ARRAY[1,7,4,2,6]</tt></td><td><tt class="LITERAL">t</tt></td></tr>
<tr><td><tt class="LITERAL">&amp;&amp;</tt></td><td>重叠(有共同元素)</td><td><tt class="LITERAL">ARRAY[1,4,3] &amp;&amp; ARRAY[2,1]</tt></td><td><tt class="LITERAL">t</tt></td></tr>
<tr><td><tt class="LITERAL">||</tt></td><td>数组与数组连接</td><td><tt class="LITERAL">ARRAY[1,2,3] || ARRAY[4,5,6]</tt></td><td><tt class="LITERAL">{1,2,3,4,5,6}</tt></td></tr>
<tr><td><tt class="LITERAL">||</tt></td><td>数组与数组连接</td><td><tt class="LITERAL">ARRAY[1,2,3] || ARRAY[[4,5,6],[7,8,9]]</tt></td><td><tt class="LITERAL">{{1,2,3},{4,5,6},{7,8,9}}</tt></td></tr>
<tr><td><tt class="LITERAL">||</tt></td><td>元素与数组连接</td><td><tt class="LITERAL">3 || ARRAY[4,5,6]</tt></td><td><tt class="LITERAL">{3,4,5,6}</tt></td></tr>
<tr><td><tt class="LITERAL">||</tt></td><td>数组与元素连接</td><td><tt class="LITERAL">ARRAY[4,5,6] || 7</tt></td><td><tt class="LITERAL">{4,5,6,7}</tt></td></tr>
</tbody>
</table>
</div>
<p>数组比较是使用默认的 B-Tree 比较函数对所有元素逐一进行比较的。多维数组的元素按照行顺序进行访问(最后的下标变化最快)。如果两个数组的内容相同但维数不等，那么决定排序顺序的首要因素将是维数(原文：the first difference in the dimensionality information determines the sort order)，这与 PostgreSQL 8.2 之前的版本不同：老版本认为内容相同的两个数组相等，即使它们的维数或下标范围并不相同。</p>
<p>参阅<a href="arrays.html">节8.10</a>获取有关数组操作符行为的更多细节。</p>
<p><a href="functions-array.html#ARRAY-FUNCTIONS-TABLE">表9-36</a>显示了可以用于数组类型的函数。参阅<a href="arrays.html">节8.10</a>获取更多信息以及使用这些函数的例子。</p>
<div class="TABLE"><a name="ARRAY-FUNCTIONS-TABLE"></a>
<p><b>表9-36. <tt class="TYPE">array</tt> 函数</b></p>
<table border="1" class="CALSTABLE"><col><col><col><col><col>
<thead>
<tr><th>函数</th><th>返回类型</th><th>描述</th><th>例子</th><th>结果</th></tr>
</thead>
<tbody> <tr><td><tt class="LITERAL"><code class="FUNCTION">array_append</code>(<tt class="TYPE">anyarray</tt>, <tt class="TYPE">anyelement</tt>) </tt></td><td><tt class="TYPE">anyarray</tt></td><td>向数组末尾添加元素</td><td><tt class="LITERAL">array_append(ARRAY[1,2], 3)</tt></td><td><tt class="LITERAL">{1,2,3}</tt></td></tr> <tr><td><tt class="LITERAL"><code class="FUNCTION">array_cat</code>(<tt class="TYPE">anyarray</tt>, <tt class="TYPE">anyarray</tt>) </tt></td><td><tt class="TYPE">anyarray</tt></td><td>连接两个数组</td><td><tt class="LITERAL">array_cat(ARRAY[1,2,3], ARRAY[4,5])</tt></td><td><tt class="LITERAL">{1,2,3,4,5}</tt></td></tr> <tr><td><tt class="LITERAL"><code class="FUNCTION">array_dims</code>(<tt class="TYPE">anyarray</tt>) </tt></td><td><tt class="TYPE">text</tt></td><td>返回数组维数的文本表示</td><td><tt class="LITERAL">array_dims(ARRAY[[1,2,3], [4,5,6]])</tt></td><td><tt class="LITERAL">[1:2][1:3]</tt></td></tr> <tr><td><tt class="LITERAL"><code class="FUNCTION">array_lower</code>(<tt class="TYPE">anyarray</tt>, <tt class="TYPE">int</tt>) </tt></td><td><tt class="TYPE">int</tt></td><td>返回数组维数的下界</td><td><tt class="LITERAL">array_lower('[0:2]={1,2,3}'::int[], 1)</tt></td><td><tt class="LITERAL">0</tt></td></tr> <tr><td><tt class="LITERAL"><code class="FUNCTION">array_prepend</code>(<tt class="TYPE">anyelement</tt>, <tt class="TYPE">anyarray</tt>) </tt></td><td><tt class="TYPE">anyarray</tt></td><td>向数组开头添加元素</td><td><tt class="LITERAL">array_prepend(1, ARRAY[2,3])</tt></td><td><tt class="LITERAL">{1,2,3}</tt></td></tr> <tr><td><tt class="LITERAL"><code class="FUNCTION">array_to_string</code>(<tt class="TYPE">anyarray</tt>, <tt class="TYPE">text</tt>) </tt></td><td><tt class="TYPE">text</tt></td><td>使用分隔符将数组元素连接为字符串</td><td><tt class="LITERAL">array_to_string(ARRAY[1, 2, 3], '~^~')</tt></td><td><tt class="LITERAL">1~^~2~^~3</tt></td></tr><tr><td><tt class="LITERAL"><code class="FUNCTION">array_upper</code>(<tt class="TYPE">anyarray</tt>, <tt class="TYPE">int</tt>) </tt></td><td><tt class="TYPE">int</tt></td><td>返回数组维数的上界</td><td><tt class="LITERAL">array_upper(ARRAY[1,2,3,4], 1)</tt></td><td><tt class="LITERAL">4</tt></td></tr> <tr><td><tt class="LITERAL"><code class="FUNCTION">string_to_array</code>(<tt class="TYPE">text</tt>, <tt class="TYPE">text</tt>) </tt></td><td><tt class="TYPE">text[]</tt></td><td>使用指定的分隔符把字符串分裂成数组元素</td><td><tt class="LITERAL">string_to_array('xx~^~yy~^~zz', '~^~')</tt></td><td><tt class="LITERAL">{xx,yy,zz}</tt></td></tr>
</tbody>
</table>
</div>
</div>
<div>
<hr align="LEFT" width="100%">
<table summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0">
<tr><td width="33%" align="left" valign="top"><a href="functions-conditional.html" accesskey="P">后退</a></td><td width="34%" align="center" valign="top"><a href="index.html" accesskey="H">首页</a></td><td width="33%" align="right" valign="top"><a href="functions-aggregate.html" accesskey="N">前进</a></td></tr>
<tr><td width="33%" align="left" valign="top">条件表达式</td><td width="34%" align="center" valign="top"><a href="functions.html" accesskey="U">上一级</a></td><td width="33%" align="right" valign="top">聚集函数</td></tr>
</table>
</div>
</body></html>